package com.stox.ui.javafx.charting.model.plot;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.stox.dto.AbstractModel;
import com.stox.dto.IndicatorModel;
import com.stox.indicator.VolumeByPrice;
import com.stox.indicator.VolumeByPrice.Configuration;
import com.stox.indicator.VolumeByPrice.Unit;
import com.stox.ui.javafx.charting.model.uiunit.VolumeByPriceUiUnit;
import com.stox.ui.javafx.charting.view.ChartingView;
import com.stox.ui.javafx.charting.widget.Chart;

@Component("volumeByPriceOverlayPlot")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class VolumeByPriceOverlayPlot extends AbstractPlot<Unit, 
																	Chart<VolumeByPriceOverlayPlot>, 
																	VolumeByPriceUiUnit, 
																	VolumeByPriceOverlayPlot> {

	public VolumeByPriceOverlayPlot(AbstractModel model) {
		super(model);
	}

	@Override
	protected void createUiUnits() {
		
	}

	@Override
	public void paint() {
		getUiUnits().clear();
		double max = Double.MIN_VALUE;
		double min = Double.MAX_VALUE;
		IndicatorModel model = (IndicatorModel)getModel();
		Configuration configuration = (Configuration)model.getConfiguration();
		VolumeByPrice volumeByPrice = (VolumeByPrice)model.getIndicator();
		ChartingView view = getChart().getChartingView(); 
		Unit[] units = volumeByPrice.getValues(configuration, view.getPrimaryChart().getFrontPlot().getData().subList(view.getStartIndex(), view.getEndIndex()));
		for(int index = 0; index < units.length; index++){
			getUiUnits().add(new VolumeByPriceUiUnit(units[index], this, index));
			double sum = units[index].getUpVolume() + units[index].getDownVolume();
			if(max < sum){
				max = sum;
			}
			if(min > sum){
				min = sum;
			}
		}
		for(VolumeByPriceUiUnit uiUnit : getUiUnits()){
			uiUnit.paint(max, min);
		}
	}


}
